<?php
namespace Pc\Controller;
use Think\Controller;
/**
 * 移动端人员查询公共公共控制器 CommonController.class.php
 */
class CommonController extends Controller{
    /* 属性声明 */
    protected $redis = null;
    protected $corpid = 'wx354598b372f7261a';
    //用户主配置文件
    protected $main_conf_file = null;
    //用户顶级子集排序文件
    protected $top_sort_conf_file = null;
    //原始顶级子集文件
    protected $ori_top_file = "./Org/Data/Original_top_list.json";

    /* 方法声明 */
    /*
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
        //顶级子集排序文件
        $this->top_sort_conf_file = "./Org/Data/UserData/top_sort/" . $this->corpid . '.conf';
        //初始化全局属性
        $this->main_conf_file = "./Org/Data/UserData/main_conf/{$this->corpid}_main.conf";
        //初始化前台js字典文件
        $this -> dic_file = './Org/Data/Dic_js/'.$this->corpid.'_dic.js';
        //树形字典
        $this->layer_file = './Org/Data/Layer_js/' . $this->corpid . '_layer.js';

        //初始化连接redis数据库
        $this -> redis = connectRedis();
        //拉取字典数据
//        $this->getInitDic();
//        //拉取扩展信息数据
//        $this->getKzConf();
//        //拉取时间验证数据
//        $this->getTimeZdConf();
    }

    //获取字典信息
    private function getInitDic(){
        $cmd = 'hr.getDictionary';
        $data = json_encode(['YGBM'=>'ok']);
        $list = A('Aws')->requestAws2($cmd,$data);
        $data = $list['data'];
        //dump($list);exit;
        unlink('./Org/Data/init_dic.json');
        file_put_contents('./Org/Data/init_dic.json',json_encode($data));
        //处理树形字典数据
        $this->treeDic($data);
        unlink('./Org/Data/Dic_js/'.$this->corpid.'_dic.js');
        unlink('./Org/Data/Layer_js/'.$this->corpid.'_layer.js');
    }

    /***
     * 处理树形结构数据
     *
     */
    private function treeDic($data){
        //遍历数据生成特定格式
        $result = [];
        foreach ($data['CASC.HR.DICT.ALL'] as $k => $v) {
            if($v['layer']){
                $result[$k] = $v;
            }
        }
	    //dump($result);exit;
        foreach($result as $k=>$v){
            $result[$k]=$this->getSon($v['subitem']);
        }
        unlink('./Org/Data/tree.json');
        file_put_contents('./Org/Data/tree.json',json_encode($result));
    }

    /**
     * 找子集
     */
    private function getSon($data){
        $list=[];
        foreach ($data as $k=>$v){
            if($v['mainid'] == ''){
                $list[$k]['name'] = $v['caption'];
                $list[$k]['id'] = $v['value'];
                $list[$k]['pId'] = 0;
            }else{
                $list[$k]['name'] = $v['caption'];
                $list[$k]['id'] = $v['value'];
                $list[$k]['pId'] = $v['mainid'];
            }
        }
        return $list;
    }
    //获取扩展配置信息
    private function getKzConf(){
        $cmd = 'hr.getFlieApi';
        $data = json_encode(['YGBM'=>'ok']);
        $list = A('Aws')->requestAws2($cmd,$data);
        $data = json_encode($list['data']);
        //dump($data);exit;
        unlink('./Org/Data/kz_conf.json');
        file_put_contents('./Org/Data/kz_conf.json',$data);
    }

    //获取时间字段验证配置信息
    private function getTimeZdConf(){
        $cmd = 'hr.getVerifyApi';
        $data = json_encode(['YGBM'=>'ok']);
        $list = A('Aws')->requestAws2($cmd,$data);
        $data = json_encode($list['data']);
        //dump($data);exit;
        unlink('./Org/Data/sj_zd_conf.json');
        file_put_contents('./Org/Data/sj_zd_conf.json',$data);
    }

    //aws接口获取员工信息
    public function getAwsUuidinfo($uuid){
        $this -> redis -> select(10);
        if(!$this->redis->hexists('user_list', $uuid)){
            $cmd = 'hr.getYgxx2';
            $data = json_encode(['YGBM'=>"$uuid"]);
            $list = A('Pc/Aws')->requestAws2($cmd,$data);
            //dump($list);exit;
            if($list['type'] == 0){
                $msg = $list['msg'];
                $this->error($msg);
            }else if($list['type'] == 1){
                $msg = $list['msg'];
                $this->error($msg);
            }else if($list['type'] == 2){
                $userinfo = $list['data'];
                $this -> redis -> select(10);
                $this->redis->hset('user_list',$uuid,json_encode($userinfo));
                //写入配置文件
//                $this->putConfFile($userinfo);
            }
//            $file = file_get_contents('./Org/origin.json');
//            $this->redis->hset('user_list',$uuid,$file);
        }
    }

    /**
     * 获取首页导航子集数据
     */
    protected function getMenuList(){
        //顶级子集原始文件
        $buffer_list = file_get_contents($this->ori_top_file);
        $buffer_list = json_decode($buffer_list, true);
        $buffer_list_keys = array_keys($buffer_list);

        //检测是否存在自定义顶级子集排序显示配置文件
        if (file_exists($this->top_sort_conf_file)) {
            //初始化数据缓冲区
            $buffer_list_conf = unserialize(file_get_contents($this->top_sort_conf_file));
            //dump($buffer_list);exit;
            foreach($buffer_list_conf as $k=>$v){
                if(!in_array($k,$buffer_list_keys)){
                    unset($buffer_list_conf[$k]);
                }else{
                    $buffer_list_conf[$k]['caption'] = $buffer_list[$k]['caption'];
                    $buffer_list_conf[$k]['name'] = $buffer_list[$k]['name'];
                }
            }
            $buffer_list = $buffer_list_conf;
        } else {
            //不存配置文件  批量添加显示状态数据
            foreach ($buffer_list as $k => $v) {
                $buffer_list[$k]['display'] = true;
            }
        }
        return $buffer_list;
    }

    /**
     * 生成字典数据
     */
    public function createDic(){
        $f1 = file_exists($this -> dic_file);
        $f2 = file_exists($this -> layer_file);
        if(!$f1 || !$f2 ){
            //获取通用字典
            $dic = $this -> dic();
            //生成前台js json文件 降低前台渲染数据压力
            $dic_file_data = 'var dic=' . $dic[1];
            $dic_file = './Org/Data/Dic_js/'.$this->corpid.'_dic.js';

            //生成树形字典文件
            $layer_file = './Org/Data/Layer_js/' . $this->corpid . '_layer.js';
            $this->layer_conf_file = './Org/Data/tree.json';
            $layer_file_data = 'var layer_js=' . file_get_contents($this->layer_conf_file);
            //检测通用字典js文件是否存在
            file_put_contents($dic_file, $dic_file_data);
            file_put_contents($layer_file, $layer_file_data);
        }
        $this -> dic = '/Org/Data/Dic_js/'.$this->corpid.'_dic.js';
        $this -> layer = '/Org/Data/Layer_js/' . $this->corpid . '_layer.js';
    }

    /**
     * 生成字典数据
     */
    public function dic(){
        $this -> user_dic_file = './Org/Data/UserData/dic_conf/'.$this->corpid.'_dic_conf.conf';
        //1.检测是否存在用户自定义字典配置
        if(file_exists($this->user_dic_file)){
            $data = json_decode(file_get_contents($this->user_dic_file),true);
            return [1,file_get_contents($this->user_dic_file)];
        }else{
            //2.返回默认字典数据
            $data = json_decode(file_get_contents('./Org/Data/init_dic.json'),true)['CASC.HR.DICT.ALL'];
            return [0,json_encode($data)];
        }
    }


    public function getUserInfo($uuid){
        $this -> redis -> select(10);
        if(!$data = $this -> redis -> hget('user_list',$uuid)){
            $this -> error('未匹配到您的相关信息!');
        }
        $data = json_decode($data,true);
        $userinfo = $data['information'];
        //dump($userinfo);exit;
        return $userinfo;
    }

    /**
     * 获取指定子集数据
     * @param array $userinfo 成员数据
     * @param string $menu 子集名称
     * @return array 返回指定菜单过滤后子集数据
     */
    public function getSublist($userinfo,$menu){
        $item = $userinfo[$menu];
        //dump($userinfo);exit;
        //检测是否存在主配置文件
        if(file_exists($this->main_conf_file)){
            $conf_info = unserialize(file_get_contents($this->main_conf_file));
            $conf = $conf_info[$menu];
            //dump($conf);

            //0.过滤标准
            $filterlist = $conf['sublist'];
            //dump($filterlist);exit;

            //1.过滤sublist
            $sublist = $item['sublist'];
            $sublist_keys = array_keys($sublist);
            //dump($sublist);exit;
            $sublist_buf = [];
            //过滤子集显示项 + 排序操作
            foreach($filterlist as $k => $v){
                if(in_array($v,$sublist_keys)){
                    $sublist_buf[$v] = $sublist[$v];
                }
            }
            //dump($sublist_buf);exit;
            //2.过滤subvalue
            $subvalue = $item['subvalue'];
            foreach($subvalue as $k => $v){
                $info = $v['info'];
                $info_keys = array_keys($info);
                //定义临时数据缓冲区
                $subvalue_buf = [];
                foreach($filterlist as $k2 => $v2){
                    if(in_array($v2,$info_keys)){
                        $subvalue_buf[$v2] = $info[$v2];
                    }
                }
                $subvalue[$k]['info'] = $subvalue_buf;
            }
            //dump($subvalue);exit;
            //3.数据重新组合
            $item['sublist'] = $sublist_buf;
            $item['subvalue'] = $subvalue;
        }
        //dump($item);exit;
        //返回最终数据
        return $item;
    }

    //获取附件信息
    public function getFileInfo($uuid,$menu,$file_zd,$dat){
        //先从redis里取出当前字段的附件信息
        $this->redis->select(9);
        //$this->redis->hdel('file_'.$uuid,$menu);
        $file_info = $this->redis->hget('file_'.$uuid,$menu);
        if($file_info){
            $file_info = unserialize($file_info);
        }else{
            //不存在调用aws库获取 循环每个字段取出相应的附件信息
            $file_info = [];
            $cmd = 'hr.getFilesById';
            if($menu == 'BO_CASC_HR_YGRS'){
                $info = [
                    'uuid' => $uuid,
                    'boname' => $menu,
                    'offset' => 0,
                    'zd_name' => $file_zd,
                ];
                $json_info = json_encode($info);
                $result = A('Pc/Aws')->requestAws2($cmd,$json_info);
                //$result = $this->_get_file1($info);
                if($result['type'] == 0){
                    $this->error('获取附件信息失败!');
                    exit;
                }else{
                    $file_info = $result['data'];
                }
            }else{
                foreach($dat['subvalue'] as $k=>$v){
                    $info = [
                        'uuid' => $uuid,
                        'boname' => $menu,
                        'offset' => $v['awsid'],
                        'zd_name' => $file_zd,
                    ];
                    //dump($info);exit;
                    $json_info = json_encode($info);
                    $result = A('Pc/Aws')->requestAws2($cmd,$json_info);
                    //$result = $this->_get_file2($info);
                    if($result['type'] == 0){
                        $this->error('获取附件信息失败!');
                        exit;
                    }else{
                        $file_info[$k] = $result['data'][$v['awsid']];
                    }
                }
            }
            $this->redis->select(9);
            $this->redis->hset('file_'.$uuid,$menu,serialize($file_info));
        }

        if($menu == 'BO_CASC_HR_YGRS'){
            foreach($file_zd as $k=>$v){
                $dat['sublist'][$v]['value'] = $file_info[0][$v];
            }
        }else{
            foreach($dat['subvalue'] as $k=>$v) {
                foreach($file_zd as $k1=>$v1){
                    $dat['subvalue'][$k]['info'][$v1]['value'] = $file_info[$k][$v1];
                }
            }

        }
        return $dat;
    }

    //写入配置文件
    private function putConfFile($userinfo){
        $buffer = [];
        foreach($userinfo['information'] as $k => $v){
            $buffer[$k]['caption'] = $v['caption'];
            $buffer[$k]['name'] = $k;
        }
        unlink('./Org/origin.json');
        unlink('./Org/Data/Original_top_list.json');
        unlink('./Org/Data/init_subitem.json');
        unlink('./Org/Data/Original_name_submodel.json');
        file_put_contents('./Org/origin.json',json_encode($userinfo));
        //顶级子集文件
        file_put_contents('./Org/Data/Original_top_list.json',json_encode($buffer));
        //员工json文件
        file_put_contents('./Org/Data/init_subitem.json',json_encode($userinfo));
        //员工json去掉information文件
        file_put_contents('./Org/Data/Original_name_submodel.json',json_encode($userinfo['information']));
    }

    //ajax删除上传文件
    public function removeFile(){
        $data = I('post.');
        $uuid = $data['uuid'];
        $menu = $data['menu'];
        $offset = $data['offset'];
        $zd_name = $data['zd_name'];
        $filename = $data['filename'];

        $this->redis->select(9);
        $file_info = unserialize($this->redis->hget('file_'.$uuid,$menu));
        if( $file_info[$offset][$zd_name][$filename]['status'] == 2){
            unlink('.'.$file_info[$offset][$zd_name][$filename]['href']);
            unset($file_info[$offset][$zd_name][$filename]);
        }else{
            $file_info[$offset][$zd_name][$filename]['status'] = 4;
            $this->redis->select(10);
            $info = json_decode($this->redis->hget('user_list',$uuid),true);
            $userinfo = $info['information'];
            if($menu == 'BO_CASC_HR_YGRS'){
                $userinfo[$menu]['sublist'][$zd_name]['change_type'] = 1;
                $userinfo[$menu]['change_type'] = 1;
            }else {
                $userinfo[$menu]['subvalue'][$offset]['info'][$zd_name]['change_type'] = 1;
                $userinfo[$menu]['subvalue'][$offset]['change_type'] = 1;
                $userinfo[$menu]['change_type'] = 1;
                $userinfo[$menu]['status']['update'][] = $offset;
                $userinfo[$menu]['status']['update'] = array_unique($userinfo[$menu]['status']['update']);
            }
            $info['information'] = $userinfo;
            $this->redis->hset('user_list',$uuid,json_encode($info));
        }
        $this->redis->select(9);
        $info = $this->redis->hset('file_'.$uuid,$menu,serialize($file_info));
        if($info === 0 ){
            $this->ajaxReturn(true);
        }else{
            $this->ajaxReturn(false);
        }
    }


    /**
     * 空方法操作处理
     */
    public function _empty(){
        $this -> error('未知操作:'.ACTION_NAME);
    }

}
